package pe.hongi.frist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class MyDeskDetail_DBAdapter {

    private static final String DATABASE_NAME = "MyDeskDetail.db";
    private static final String DATABASE_TABLE = "MyDeskDetail_tbl_manger";
    private static final int    DATABASE_VERSION = 1;

    
    private static final String DATABASE_TABLE_CREATE =
        "CREATE TABLE " + DATABASE_TABLE +
        " (id INTEGER PRIMARY KEY AUTOINCREMENT," +
        "MyDeskDetail_url TEXT NOT NULL," +
        "MyDeskDetail_time_all TEXT NOT NULL," +
        "MyDeskDetail_time_num NOT NULL," +
        "MyDeskDetail_complete TEXT NOT NULL);";
    
    
    private static final String DATABASE_TABLE_DROP =
        "DROP TABLE IF EXISTS " + DATABASE_TABLE;

    private SQLiteDatabase mDb; 		
    private DatabaseHelper mDbHelper; 	
    private Context context;
    
    public MyDeskDetail_DBAdapter(Context _context) {
        context = _context;

       
        mDbHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    
    private class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(
            Context context, String name, CursorFactory factory, int version) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

       
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_TABLE_CREATE);
        }
 
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w("MY_TAG", "Upgrading DB from version" + oldVersion + " to" +
                newVersion + ", which will destroy all old data");

            _db.execSQL(DATABASE_TABLE_DROP);
            onCreate(_db);
        }

    }

    
    public MyDeskDetail_DBAdapter open() throws SQLException {
     
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

   
    public void close() {
        
        mDb.close();
    }

 
    
    
    public long insertEntry(String MyDeskDetail_url, String MyDeskDetail_time_all, String MyDeskDetail_time_num, String MyDeskDetail_complete) {
        ContentValues values = new ContentValues();
        values.put("MyDeskDetail_url", MyDeskDetail_url);
        values.put("MyDeskDetail_time_all", MyDeskDetail_time_all);
        values.put("MyDeskDetail_time_num", MyDeskDetail_time_num);
        values.put("MyDeskDetail_complete", MyDeskDetail_complete);
        return mDb.insert(DATABASE_TABLE, null, values);
    }

    

    
    
    public boolean updateEntry(long rowID, String MyDeskDetail_url, String MyDeskDetail_time_all, String MyDeskDetail_time_num, String MyDeskDetail_complete) {
        ContentValues values = new ContentValues();
        values.put("MyDeskDetail_url", MyDeskDetail_url);
        values.put("MyDeskDetail_time_all", MyDeskDetail_time_all);
        values.put("MyDeskDetail_time_num", MyDeskDetail_time_num);
        values.put("MyDeskDetail_complete", MyDeskDetail_complete);
        return mDb.update(DATABASE_TABLE, values, "id=" + rowID, null) > 0;
    }
    
    

    public boolean deleteEntry(long rowID) {
        return mDb.delete(DATABASE_TABLE, "id=" + rowID, null) > 0;
    }

   
    public void deleteEntry_for_MyDeskDetail_url(String MyDeskDetail_url) {
    	mDb.execSQL("DELETE FROM " + DATABASE_TABLE + " WHERE MyDeskDetail_url=" + "'" + MyDeskDetail_url + "'");
    }
    
    
    public void deleteEntry_for_MyDeskDetail_time_all(String MyDeskDetail_time_all) {
    	mDb.execSQL("DELETE FROM " + DATABASE_TABLE + " WHERE MyDeskDetail_time_all=" + "'" + MyDeskDetail_time_all + "'");
    }

   
    
    
    
    
    public int countEntry_for_MyDeskDetail_url(String MyDeskDetail_url) { 

    	String Sql = "SELECT * FROM " + DATABASE_TABLE + " WHERE MyDeskDetail_url=" + "'" + MyDeskDetail_url + "'";
    	Cursor	pCur = mDb.rawQuery(Sql, null);
    	int Count_Item = pCur.getCount();
    	pCur.close();
    	return Count_Item; 
    }
    
   
    

    
   
    
   public Cursor getEntry_for_MyDeskDetail_url(String MyDeskDetail_url)throws SQLException
	{
		Cursor mCursor = mDb.query(true, DATABASE_TABLE,
				new String[]{"id", "MyDeskDetail_url", "MyDeskDetail_time_all", "MyDeskDetail_time_num", "MyDeskDetail_complete"},
				"MyDeskDetail_url=" + "'" + MyDeskDetail_url + "'",
				null, null, null, null, null);
		
		if(mCursor != null) mCursor.moveToFirst();

        return mCursor;
		
	}
    
 
   
    public Cursor getAllEntries() {
        return mDb.query(DATABASE_TABLE, new String[]{"id", "MyDeskDetail_url", "MyDeskDetail_time_all", "MyDeskDetail_time_num", "MyDeskDetail_complete"},
                               null, null, null, null, null, null);
    }


}
